<template>
  <basic-container>
    <div class="card-border">
      <card-header :header-data="headerData"></card-header>
      <avue-crud :data="scheduleData"
                 :option="option"
                 :page="page"
                 :table-loading="tableLoading"
                 @size-change="sizeChange"
                 @current-change="currentChange"
                 @search-change="searchChange"
                 @search-reset="searchReset"
                 class="avuecrud">
        <template slot="menuRight">
          <el-button type="warning" size="small" @click="exportToDoAdjustData()">导出</el-button>
        </template>
        <template slot-scope="scope"
                  slot="menu">
          <div class="menu-title"
               @click.stop="todoTask(scope.row,scope.index)">
            <i class="iconfont iconview icon-primary"></i>
            <p class="menu-title-text">办理</p>
          </div>
          <div class="menu-title" @click.stop="assign(scope.row,scope.index)">
            <i class="iconfont iconalter icon-warning"></i>
            <p class="menu-title-text">转办</p>
          </div>
        </template>
      </avue-crud>
      <el-dialog title="流程图"
                 :visible.sync="showPicDialog"
                 width="1200px">
        <img :src="actPicUrl"
             width="100%">
      </el-dialog>
    </div>
    <el-dialog title="修改调整建议"
               :visible.sync="dialogVisible"
               width="800px">
      <el-form label-width="130px"
               :model="reportOpinionForm"
               style="padding-right: 30px;"
               :rules="reportOpinionFormRules"
               ref="reportOpinionForm">
        <el-row>
          <el-col :span="22">
            <el-form-item class="form-item"
                          label="调整建议 :"
                          prop="content">
              <el-input type="textarea"
                        :autosize="{ minRows: 4, maxRows: 4}"
                        placeholder="请输入调整建议"
                        v-model="reportOpinionForm.content"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="22">
            <el-form-item class="form-item"
                          label="相关附件:"
                          :required="true">
              <el-upload class="upload-demo"
                         ref="uploadrelated"
                         action="string"
                         :http-request="submitUploadMixin"
                         :on-preview="handlePreviewMixin"
                         :on-remove="handleRemoveMixin"
                         :file-list="fileListMixin"
                         :auto-upload="true"
                         :on-change="fileChangeMixin">
                <el-button slot="trigger"
                           size="small"
                           type="primary">点击上传</el-button>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button type="primary"
                   @click="comitYes()">报 送</el-button>
        <el-button @click="cancel()">取 消</el-button>
      </div>
    </el-dialog>
    <assign :visible="assignVisible" :task-id="taskIdAssign" @cancelAssign="cancelAssign"></assign>
  </basic-container>
</template>
<script>
import { toDoReportList, taskTodo, submitReportOpinionEdit, submitReportOpinionImpt, submitReportOpinionCommon ,exportToDoAdjustDataExcel} from "@/api/pim/pim";
import { PROJECT_DIC, REVISE_STATES_DIC } from "@/const/dict";
import { reportOpinionFormRules } from "@/const/infoRules";
import relatedfile from "@/mixins/relatedfile";

export default {
  name: "todoAdjust",
  mixins: [relatedfile],
  created () {
    this.getToDoList(this.page);
    window.resultDetail = this.resultDetail;
  },
  data () {
    return {
      headerData: {
        title: '修订调整'
      },
      actPicUrl: '',
      showPicDialog: false,
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      tableLoading: false,
      scheduleData: [],
      option: {
        page: true,
        addBtn: false,
        editBtn: false,
        addRowBtn: false,
        indexLabel: "序号",
        index: true,
        indexWidth:70,
        cellBtn: false,
        delBtn: false,
        columnBtn: false,
        refreshBtn: false,
        filterBtn: false,
        searchBtn: false,
        menuWidth: 150,
        keyId: "id",
        column: [
          {
            label: "规划成果名称",
            prop: "achievementTitle",
            cell: true,
            overHidden: true,
            formatter: function (row, value, label, column) {
              return '<a style="cursor: pointer;" onclick="resultDetail(\'' + row.achievementId + '\')">' + value + '</a>';
            },
          },
          {
            label: "名称查询",
            prop: "titleLike",
            cell: true,
            search: true,
            hide: true,
            overHidden: true,
            formatter: function (row, value, label, column) {
              return '<a style="cursor: pointer;" onclick="resultDetail(\'' + row.achievementId + '\')">' + value + '</a>';
            },
          },
          {
            label: "规划类别",
            prop: "category",
            cell: true,
            width: 120,
            dicData: PROJECT_DIC.CATEGORY,
          },
          {
            label: "状态",
            prop: "state",
            cell: true,
            width: 120,
            dicData: REVISE_STATES_DIC.STATE
          },
        ]
      },
      dialogVisible: false,
      reportOpinionForm: {
        achievementId: null,
        content: '',
        contentFileIds: [],
        reviseId: null,
      },
      reportOpinionFormRules: reportOpinionFormRules,
      taskIdTemp: null,
      editType: null,
      assignVisible: false,
      taskIdAssign: null,
      titleLikeSearch:''

    };
  },
  methods: {
    // 成果详情跳转
    resultDetail (achievementId) {
      let src = `/planning/achievement/detail?id=${achievementId}`;
      this.$router.push({
        path: src
      })
    },
    getToDoList (page, params) {
      this.tableLoading = true;
      taskTodo(Object.assign({
        current: page.currentPage,
        size: page.pageSize,
      }, params)).then(response => {
        this.scheduleData = response.data.data.records;
        this.page.total = response.data.data.total;
        this.tableLoading = false;
        this.$emit("getTotal", response.data.data.total)
      })
    },
    searchReset () {
      this.titleLikeSearch = '';
      let params = {
        titleLike:this.titleLikeSearch,
      };
      this.getToDoList(this.page,params);
    },
    //搜索回调
    searchChange (params) {
      this.page.currentPage = 1
      this.getToDoList(this.page, this.dealSearchParam(params));
    },
    // 处理搜索过滤
    dealSearchParam (params) {
      if (params.titleLike) {
        params.titleLike = params.titleLike;
      }
      this.titleLikeSearch = params.titleLike;
      return params;
    },
    currentChange(currentPage) {
      var page = this.page;
      page.currentPage = currentPage;
      let params = {
        titleLike:this.titleLikeSearch,
      };
      this.getToDoList(page,params);
    },
    sizeChange(pageSize) {
      var page = this.page;
      page.pageSize = pageSize;
      let params = {
        titleLike:this.titleLikeSearch,
      };
      this.getToDoList(page,params);
    },
    todoTask (row, index) {
      switch (row.state) {
        case '1': this.developEditOpinion(row, '1'); break;//修改
        case '2': this.reviseGovernment(row); break;//政府审核
        case '3': this.reviseCommittee(row); break;//省委审核
        case '4': this.reviseCongress(row); break;//人大审议
        case '5': this.reviseEditDraft(row); break;//修改分解方案
        case '8': this.DRcheck(row); break;//发改审核（重点专项，区域）
        case '9': this.governmentApproval(row); break;//政府审批（重点专项，区域）
        case '10': this.editDraftImpt(row); break;//修改分解方案（重点专项，区域）
        case '11': this.developEditOpinion(row, '2'); break;//修改（重点专项，区域）
        case '12': this.DRcheckApproval(row); break;//发改审批（一般专项）
        case '13': this.editDraftCommon(row); break;//修改分解方案（一般专项）
        case '14': this.developEditOpinion(row, '3'); break;////修改（一般专项）
        case '15': this.editDraftRecord(row); break;//修改分解方案（备案专项）
        case '16': this.releaseAdjustDevelop(row); break;//印发（发展规划）
        case '17': this.releaseAdjustImpt(row); break;//印发（重点专项，区域）
        case '18': this.releaseAdjustCommon(row); break;//印发（一般专项）
        case '19': this.releaseAdjustCongress(row); break;//印发（备案专项规划）
        case '21': this.DRReplaceApproval(row); break;//发改代拟，发展规划中期评估报告有调整建议的
        case '22': this.govReleaseAdjust(row); break;//政府印发
        case '23': this.editDraftAdjust(row); break;//修改分解方案-有建议，发展规划中期评估报告有调整建议的
        case '24': this.reviseGovernmentTwo(row); break;//政府审核：发展规划修订调整流程中党委审核之后到这一步
      }
    },
    reviseGovernment (row) {
      let src = `/reviseGovernment/detail/${row.taskId}`;
      this.$router.push({
        path: src,
        query: {
          reviseId: row.reviseId,
          reportId: row.midReportId,
        }
      })
    },
    reviseGovernmentTwo (row) {
      let src = `/reviseGovernmentTwo/detail/${row.taskId}`;
      this.$router.push({
        path: src,
        query: {
          reviseId: row.reviseId,
          reportId: row.midReportId,
        }
      })
    },
    reviseCommittee (row) {
      let src = `/reviseCommittee/detail/${row.taskId}`;
      this.$router.push({
        path: src,
        query: {
          reviseId: row.reviseId,
          reportId: row.midReportId,
        }
      })
    },
    developEditOpinion (row, type) {
      this.dialogVisible = true;
      this.reportOpinionForm.achievementId = row.achievementId;
      this.reportOpinionForm.reviseId = row.reviseId;
      this.taskIdTemp = row.taskId;
      this.editType = type;
    },
    reviseCongress (row) {
      let src = `/reviseCongress/detail/${row.taskId}`;
      this.$router.push({
        path: src,
        query: {
          reviseId: row.reviseId,
          reportId: row.midReportId,
        }
      })
    },
    reviseEditDraft (row) {
      let src = `/reviseEditDraft/detail/${row.taskId}`;
      this.$router.push({
        path: src,
        query: {
          reviseId: row.reviseId,
          reportId: row.midReportId,
          decomposeDraftId: row.decomposeDraftId,
        }
      })
    },
    DRcheck (row) {
      let src = `/DRcheck/detail/${row.taskId}`;
      this.$router.push({
        path: src,
        query: {
          reviseId: row.reviseId,
          decomposeDraftId: row.decomposeDraftId,
        }
      })
    },
    governmentApproval (row) {
      let src = `/governmentApproval/detail/${row.taskId}`;
      this.$router.push({
        path: src,
        query: {
          reviseId: row.reviseId,
          decomposeDraftId: row.decomposeDraftId,
        }
      })
    },
    editDraftImpt (row) {
      let src = `/editDraftImpt/detail/${row.taskId}`;
      this.$router.push({
        path: src,
        query: {
          reviseId: row.reviseId,
          decomposeDraftId: row.decomposeDraftId,
        }
      })
    },
    DRcheckApproval (row) {
      let src = `/DRcheckApproval/detail/${row.taskId}`;
      this.$router.push({
        path: src,
        query: {
          reviseId: row.reviseId,
          decomposeDraftId: row.decomposeDraftId,
        }
      })
    },
    editDraftCommon (row) {
      let src = `/editDraftCommon/detail/${row.taskId}`;
      this.$router.push({
        path: src,
        query: {
          reviseId: row.reviseId,
          decomposeDraftId: row.decomposeDraftId,
        }
      })
    },
    editDraftRecord (row) {
      let src = `/editDraftRecord/detail/${row.taskId}`;
      this.$router.push({
        path: src,
        query: {
          reviseId: row.reviseId,
          decomposeDraftId: row.decomposeDraftId,
        }
      })
    },
    editDraftAdjust (row) {
      let src = `/editDraftAdjust/detail/${row.taskId}`;
      this.$router.push({
        path: src,
        query: {
          reviseId: row.reviseId,
          decomposeDraftId: row.decomposeDraftId,
        }
      })
    },
    releaseAdjustDevelop (row) {
      let src = `/releaseAdjust/detail/${row.taskId}`;
      this.$router.push({
        path: src,
        query: {
          reviseId: row.reviseId,
          decomposeDraftId: row.decomposeDraftId,
        }
      })
    },
    releaseAdjustImpt (row) {
      let src = `/releaseAdjustImpt/detail/${row.taskId}`;
      this.$router.push({
        path: src,
        query: {
          reviseId: row.reviseId,
          decomposeDraftId: row.decomposeDraftId,
        }
      })
    },
    releaseAdjustCommon (row) {
      let src = `/releaseAdjustCommon/detail/${row.taskId}`;
      this.$router.push({
        path: src,
        query: {
          reviseId: row.reviseId,
          decomposeDraftId: row.decomposeDraftId,
        }
      })
    },
    releaseAdjustCongress (row) {
      let src = `/releaseAdjustCongress/detail/${row.taskId}`;
      this.$router.push({
        path: src,
        query: {
          reviseId: row.reviseId,
          decomposeDraftId: row.decomposeDraftId,
        }
      })
    },
    //发改代拟
    DRReplaceApproval (row) {
      let src = `/DRReplaceApproval/detail/${row.taskId}`;
      this.$router.push({
        path: src,
        query: {
          reviseId: row.reviseId,
          achievementTitle: row.achievementTitle,
        }
      })
    },
    //政府印发
    govReleaseAdjust (row) {
      let src = `/govReleaseAdjust/detail/${row.taskId}`;
      this.$router.push({
        path: src,
        query: {
          reviseId: row.reviseId,
          achievementTitle: row.achievementTitle,
        }
      })
    },
    // 详情
    viewDetail (row, index) {
      let src = `/reviewDetail/detail/${row.reviewId}`;
      this.$router.push({
        path: src
      })
    },
    // 流程图
    flowChart (row, index) {
      this.actPicUrl =
        `/planning/registerinfo/task/view/` +
        row.taskId;
      this.showPicDialog = true;
    },
    cancel () {
      this.dialogVisible = false;
    },
    comitYes () {
      var arr = [];
      for (var item in this.fileListMixin) {
        arr.push(this.fileListMixin[item].url);
      }
      this.reportOpinionForm.contentFileIds = arr;

      if (this.reportOpinionForm.contentFileIds.length == 0) {
        this.$notify({
          title: "提醒",
          type: "warning",
          message: "请上传相关附件",
          duration: 2000
        });
        return;
      }
      this.$refs.reportOpinionForm.validate(valid => {
        if (valid) {
          this.$confirm("确定报送吗?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(() => {
            if (this.editType == '1') {
              submitReportOpinionEdit(this.taskIdTemp, this.reportOpinionForm).then(response => {
                if (response.data.code == 0) {
                  this.$notify({
                    title: "成功",
                    message: "报送成功",
                    type: "success",
                    duration: 2000
                  });
                  this.$router.go(0);
                }
              })
            } else if (this.editType == '2') {
              submitReportOpinionImpt(this.taskIdTemp, this.reportOpinionForm).then(response => {
                if (response.data.code == 0) {
                  this.$notify({
                    title: "成功",
                    message: "报送成功",
                    type: "success",
                    duration: 2000
                  });
                  this.$router.go(0);
                }
              })
            } else if (this.editType == '3') {
              submitReportOpinionCommon(this.taskIdTemp, this.reportOpinionForm).then(response => {
                if (response.data.code == 0) {
                  this.$notify({
                    title: "成功",
                    message: "报送成功",
                    type: "success",
                    duration: 2000
                  });
                  this.$router.go(0);
                }
              })
            }

          })
        }
      })
    },
    assign(row) {
      this.taskIdAssign = row.taskId;
      this.assignVisible = true;
    },
    cancelAssign() {
      this.assignVisible = false;
    },
    exportToDoAdjustData(){
      this.$confirm("确定要导出修订调整列表吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        exportToDoAdjustDataExcel().then(response => {
          let exportFileName = "修订调整列表.xlsx";
          this.download(response.data, exportFileName)
        });
      })
    },
    // 下载文件
    download(data, filename) {
      if (data == null) {

      } else {
        let url = window.URL.createObjectURL(new Blob([data]));
        let link = document.createElement('a');
        link.style.display = 'none';
        link.href = url;
        link.setAttribute('download', filename);
        document.body.appendChild(link);
        link.click()
      }
    },

  }
};
</script>

<style lang="scss" scoped>
</style>
